package com.guigu.guliware.component;

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.guigu.gulicommon.constant.MQConstant.*;

/**
 * MQ工具类
 *
 * @author 陈俊杰
 * @version 1.0
 * @since 2024/9/30 22:56
 */
@Log4j2
@Component
public class RabbitMqUtils {

    /**
     * 库存交换机
     *
     * @return 交换机
     */
    @Bean
    public Exchange stockExchange() {
        return new TopicExchange(STOCK_EVENT_EXCHANGE, true, false);
    }

    /**
     * 释放库存队列
     *
     * @return 队列
     */
    @Bean
    public Queue stockReleaseQueue() {
        return new Queue(STOCK_RELEASE_QUEUE, true, false, false);
    }

    /**
     * 库存死信队列
     *
     * @return 队列
     */
    @Bean
    public Queue stockDeadQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", STOCK_EVENT_EXCHANGE);
        args.put("x-dead-letter-routing-key", STOCK_DEAD_ROUTING_KEY);
        args.put("x-message-ttl", 120000);
        return new Queue(STOCK_DELAY_QUEUE, true, false, false, args);
    }

    /**
     * 库存锁定绑定关系
     *
     * @return 绑定关系
     */
    @Bean
    public Binding stockLockBinding() {
        return new Binding(STOCK_RELEASE_QUEUE, Binding.DestinationType.QUEUE,
                STOCK_EVENT_EXCHANGE, STOCK_LOCKED_ROUTING_KEY, null);
    }

    /**
     * 库存释放绑定关系
     *
     * @return 绑定关系
     */
    @Bean
    public Binding stockReleaseBinding() {
        return new Binding(STOCK_DELAY_QUEUE, Binding.DestinationType.QUEUE,
                STOCK_EVENT_EXCHANGE, STOCK_DEAD_ROUTING_KEY + ".#", null);
    }


}


